//
//  GNViewController.m
//  Trip
//
//  Created by dlios on 15-7-13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "GNViewController.h"
//#import "ZuiJinLiuLanCell.h"
#import "JingDianCell.h"
#import "MyCollectionReusableView.h"
#import "PlaceViewController.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
//#import "Tactics.h"

@interface GNViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation GNViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    [self netWorking];
    [self headlerData];
    
    
}

- (void)headlerData
{
    NSString *str = @"http://chanyouji.com/api/destinations.json";
    [AFNetworkHandler GetWithAFNByURL:str completion:^(id result) {
       
        self.sourceArrN = result;
        [self.collectionView reloadData];
        [self setCollection];
    }];
}

- (void)setCollection
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 15);
    flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 20);
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [self.collectionView registerClass:[JingDianCell class] forCellWithReuseIdentifier:@"ouzhou"];
    [self.collectionView registerClass:[JingDianCell class] forCellWithReuseIdentifier:@"yazhou"];
    [self.collectionView registerClass:[JingDianCell class] forCellWithReuseIdentifier:@"meizhou"];
    [self.collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PlaceViewController *placeVC = [[PlaceViewController alloc] init];
    placeVC.str = [[[[self.sourceArrN objectAtIndex:indexPath.section] objectForKey:@"destinations"] objectAtIndex:indexPath.item] objectForKey:@"id"];
//    NSLog(@"%@", placeVC.str);
    [self.navigationController pushViewController:placeVC animated:YES];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = @[@"-----亚洲-----", @"-----欧洲-----", @"-----美洲、大洋洲、非洲与南极洲-----"];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        MyCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        reuseView.myLabel.text = [arr objectAtIndex:indexPath.section];
        
        return reuseView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.bounds.size.width / 2 - 20, (self.view.bounds.size.width / 2 - 20) * 1.3);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return 6;
//    }
    if (section == 0) {
        return 14;
    }
    if (section == 1) {
        return 12;
    }
    else return 4;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        JingDianCell *yaZhou = [collectionView dequeueReusableCellWithReuseIdentifier:@"yazhou" forIndexPath:indexPath];
//        NSURL *url = self.sourceArrN[0][@"destinations"][indexPath.row][image_url];
//        [yaZhou.images setImageWithURL:[NSURL URLWithString:[[[[self.sourceArrN objectAtIndex:0] objectForKey:@"destinations"] objectAtIndex:indexPath.item] objectForKey:@"image_url"]]];
        [yaZhou.images setImageWithURL:[NSURL URLWithString:[[[[self.sourceArrN objectAtIndex:0] objectForKey:@"destinations"] objectAtIndex:indexPath.item] objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
        yaZhou.label.text = [[[[self.sourceArrN objectAtIndex:0] objectForKey:@"destinations"] objectAtIndexedSubscript:indexPath.item] objectForKey:@"name_zh_cn"];
        yaZhou.label.textColor = [UIColor whiteColor];
        yaZhou.label1.text = [[[[self.sourceArrN objectAtIndex:0] objectForKey:@"destinations"] objectAtIndexedSubscript:indexPath.item] objectForKey:@"name_en"];
        yaZhou.label1.textColor = [UIColor whiteColor];
        return yaZhou;
    }
    if (indexPath.section == 1) {
        JingDianCell *ouZhou = [collectionView dequeueReusableCellWithReuseIdentifier:@"ouzhou" forIndexPath:indexPath];
        [ouZhou.images setImageWithURL:[NSURL URLWithString:[[[[self.sourceArrN objectAtIndex:1] objectForKey:@"destinations"] objectAtIndex:indexPath.item] objectForKey:@"image_url"]]];
        ouZhou.label.text = [[[[self.sourceArrN objectAtIndex:1] objectForKey:@"destinations"] objectAtIndexedSubscript:indexPath.item] objectForKey:@"name_zh_cn"];
        ouZhou.label.textColor = [UIColor whiteColor];
        ouZhou.label1.text = [[[[self.sourceArrN objectAtIndex:1] objectForKey:@"destinations"] objectAtIndexedSubscript:indexPath.item] objectForKey:@"name_en"];
        ouZhou.label1.textColor = [UIColor whiteColor];
        return ouZhou;
    }
    if (indexPath.section == 2) {
        JingDianCell *meiZhou = [collectionView dequeueReusableCellWithReuseIdentifier:@"meizhou" forIndexPath:indexPath];
        [meiZhou.images setImageWithURL:[NSURL URLWithString:[[[[self.sourceArrN objectAtIndex:2] objectForKey:@"destinations"] objectAtIndex:indexPath.item] objectForKey:@"image_url"]]];
        meiZhou.label.text = [[[[self.sourceArrN objectAtIndex:2] objectForKey:@"destinations"] objectAtIndexedSubscript:indexPath.item] objectForKey:@"name_zh_cn"];
        meiZhou.label.textColor = [UIColor whiteColor];
        meiZhou.label1.text = [[[[self.sourceArrN objectAtIndex:2] objectForKey:@"destinations"] objectAtIndexedSubscript:indexPath.item] objectForKey:@"name_en"];
        meiZhou.label1.textColor = [UIColor whiteColor];
        return meiZhou;
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
